package business.global.pk.ddz;

import business.ddz.c2s.iclass.SDDZ_CardNumber;
import com.ddm.server.common.CommLogD;
import com.ddm.server.common.utils.Maps;
import jsproto.c2s.cclass.pk.BasePocker.PockerListType;
import jsproto.c2s.cclass.pk.BasePockerLogic;

import java.util.*;

/**
 * 资阳跑得快，设置牌
 *
 * @author Huaxing
 */
public class DDZSetCard {

    public ArrayList<Integer> leftCards = new ArrayList<Integer>(); // 扑克牌编号
    public ArrayList<Integer> backUpCards = new ArrayList<Integer>(); // 扑克牌编号
    private Random random;
    public DDZRoom zRoom;
    private Map<Integer, Integer> cardNumMap = Maps.newConcurrentMap();
    protected int cardSize = 17;

    public DDZSetCard(DDZRoom room) {
        random = new Random();
        zRoom = room;
        randomCard();
    }


    /**
     * 生成牌堆
     */
    @SuppressWarnings("unchecked")
    public void randomCard() {
        cardNumMap = Maps.newConcurrentMap();
        this.leftCards = BasePockerLogic.getRandomPockerList(1, 1, PockerListType.POCKERLISTTYPE_TWOEND);
        int totalCards;
        //勾线记牌器功能才设置剩余牌数
        for (int i = 0; i < this.leftCards.size(); i++) {
            Integer card = this.leftCards.get(i);
            int cardKey = BasePockerLogic.getCardValueEx(card);
            cardKey = (cardKey==17||cardKey==18)?cardKey-1:cardKey;
            int cardNum = cardNumMap.get(cardKey) != null ? cardNumMap.get(cardKey) + 1 : 1;
            cardNumMap.put(cardKey, cardNum);
        }
        totalCards = cardNumMap.values().stream().reduce(0, (x, y) -> x + y);
        //如果牌值不等于默认牌张数
        if (totalCards != 54) {
            CommLogD.error("The number of CARDS exceeds no equal 54");
        }
    }

    public void clean() {
        this.random = null;
        if (null != this.leftCards) {
            this.leftCards.clear();
            this.leftCards = null;
        }
        if (null != this.backUpCards) {
            this.backUpCards.clear();
            this.backUpCards = null;
        }
        if (null != this.cardNumMap) {
            this.cardNumMap.clear();
            this.cardNumMap = null;
        }
    }


    /*
     * 洗牌
     * **/
    @SuppressWarnings("unchecked")
    public void onXiPai() {
        Collections.shuffle(this.leftCards);
        this.backUpCards = (ArrayList<Integer>) this.leftCards.clone();
    }

    /**
     * 发牌
     *
     * @param cnt
     * @return
     */
    public ArrayList<Integer> popList(int cnt) {
        ArrayList<Integer> ret = new ArrayList<Integer>();
        if (this.leftCards.size() <= 0) return ret;
        for (int i = 0; i < cnt; i++) {
            if (this.leftCards.size() <= 0) return ret;
            Integer byte1 = this.leftCards.remove(random.nextInt(this.leftCards.size()));
            ret.add(byte1);
        }
        return ret;
    }

    public ArrayList<Integer> getLeftCards() {
        return leftCards;
    }

    /**
     * 记牌器减少牌数
     *
     * @param cardLst
     */
    public void subCardCount(ArrayList<Integer> cardLst) {
        //勾线记牌器功能才通知
        for (Integer byte1 : cardLst) {
            int cardValue = BasePockerLogic.getCardValueEx(byte1);
            cardValue = (cardValue==17||cardValue==18)?cardValue-1:cardValue;
            int count = this.cardNumMap.get(cardValue);
            this.cardNumMap.put(cardValue, count - 1);
        }
        notity2CardNumMap();
    }

    /**
     * 通知更新记牌器
     */
    public void notity2CardNumMap() {
        this.zRoom.getRoomPosMgr().notify2All(SDDZ_CardNumber.make(this.zRoom.getRoomID(), cardNumMap));
    }

    public Map<Integer, Integer> getCardNumMap() {
        return cardNumMap;
    }

    public int getCardSize() {
        return cardSize;
    }
}

